import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;
import realm.DefinitionRealm;

import java.security.Permission;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Description: 自定义Realm示例
 */
public class HelloShiro {

    @Test
    public void shiroLogin(){
        //导入INI配置
        DefinitionRealm realm = new DefinitionRealm();
        //构建安全管理器
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager(realm);
        //使用工具生效安全管理器
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        //使用工具获得subject主体
        Subject subject = SecurityUtils.getSubject();
        //构建账户密码
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("Bink","131");
        //使用subject主体去登录
        subject.login(usernamePasswordToken);
        //打印登录信息
        System.out.println("登录结果:"+subject.isAuthenticated());
        // 鉴权
        boolean hasRole = subject.hasRole("admin");
        System.out.println("是否是管理员:"+hasRole);
        //以is和has开头会返回布尔值
        boolean[] permitted = subject.isPermitted("user:add", "user:delete");
        for (boolean b : permitted) {
            System.out.println(b);
        }
        //以check开头的会抛出异常
        subject.checkRole("admin1");
    }

}
